package com.blank.gpstest;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.ListView;

/**
 * @author: blank
 * @date : 2022/7/22
 * @desc :
 * @url : https://gitee.com/hyx_blank
 */
public class MyListView extends ListView {
    private final Context context;

    public MyListView(Context context) {
        super(context);
        this.context = context;
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
    }

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//根据模式计算每个child的高度和宽度
//
//        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.EXACTLY);
//
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        //获取屏幕的最大宽度
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;
        int maxWidth = 1 * width / 1;
//        if(widthMode == MeasureSpec.AT_MOST && widthSize > maxWidth){
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, widthMode);
//        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//        int expandSpec = MeasureSpec.makeMeasureSpec(360, MeasureSpec.EXACTLY);
//        super.onMeasure(expandSpec, heightMeasureSpec);

    }
}
